﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Helper;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Managers;
using MvcEngine.Core.Exceptions;
using MvcEngine.Core;
using MvcEngine.Core.Content;

namespace MvcEngine.Blog.BackOffice.Filters
{
    public class PostActionFilter : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            BlogData blog = null;
            BlogPost post = null;

            var postIdStr = (string)filterContext.RouteData.Values["postId"];
            if (!string.IsNullOrEmpty(postIdStr))
            {
                post = BlogPostManager.Current.GetPost(new Guid(postIdStr));
            }


            if (post != null)
            {
                blog = BlogManager.Current.GetBlog(post.BlogId);
            }

            if (blog != null)
            {
                filterContext.ActionParameters["blogId"] = blog.Id;
                filterContext.ActionParameters["blogName"] = blog.Name;
                filterContext.RequestContext.RouteData.Values["blogId"] = blog.Id;
                filterContext.RequestContext.RouteData.Values["blogName"] = blog.Name;
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            
        }
    }
}
